﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using JoltEnvironment.Sql;

namespace WorldServer
{
    public class WorldManager
    {
        #region Fields

        /// <summary>
        /// Id of the parent cluster.
        /// </summary>
        public int ClusterID;

        /// <summary>
        /// Id of the world.
        /// </summary>
        public int ID;

        /// <summary>
        /// Name of the world.
        /// </summary>
        public string Name;

        /// <summary>
        /// IP address of the world.
        /// </summary>
        public string IP;

        /// <summary>
        /// Is world enabled?
        /// </summary>
        public int Enabled;

        /// <summary>
        /// Max amount of players allowed into world.
        /// </summary>
        public int PlayerLimit;

        /// <summary>
        /// Provides packet handling functionality to clients.
        /// </summary>
        public PacketHandler PacketHandler;

        #endregion

        #region Construct

        /// <summary>
        /// Constructs a new world manager.
        /// </summary>
        public WorldManager()
        {
            this.ClusterID = int.Parse(Program.Config.Get("World.Cluster", "1"));
            this.ID = int.Parse(Program.Config.Get("World.Id", "2"));
            this.Name = Program.Config.Get("World.Name", "Cluster");
            this.IP = Program.Config.Get("World.Ip", "127.0.0.1");
            this.Enabled = int.Parse(Program.Config.Get("World.Enabled", "1"));
            this.PlayerLimit = int.Parse(Program.Config.Get("World.PlayerLimit", "100"));

            this.PacketHandler = new PacketHandler();
        }

        #endregion

        #region Methods

        /// <summary>
        /// Adds the server to the MySQL server list.
        /// </summary>
        public void Add()
        {
            using (SqlDatabaseClient Client = Program.DatabaseManager.GetClient())
            {
                Client.AddParameter("parent_id", this.ClusterID);
                Client.AddParameter("id", this.ID);
                Client.AddParameter("name", this.Name);
                Client.AddParameter("ip", this.IP);
                Client.AddParameter("enabled", this.Enabled);
                Client.AddParameter("online_limit", this.PlayerLimit);

                Client.ExecuteQuery("DELETE FROM server_tbl WHERE id=@id;");
                Client.ExecuteQuery("INSERT INTO server_tbl VALUES(@parent_id, @id, @name, @ip, @enabled, @online_limit);");
            }
        }

        /// <summary>
        /// Removes the server from the MySQL server list.
        /// </summary>
        public void Remove()
        {
            using (SqlDatabaseClient Client = Program.DatabaseManager.GetClient())
            {
                Client.AddParameter("id", this.ID);

                Client.ExecuteQuery("DELETE FROM server_tbl WHERE id=@id;");
            }
        }

        #endregion
    }
}
